import 'package:flutter/material.dart';
import 'package:flutter_blog/model/Blog.dart';
import 'package:get/get.dart';
import 'package:logger/logger.dart';

class MenuController extends GetxController {
  @override
  void onInit() {
    super.onInit();
    //初始化数据
    updateBlogList(_currentSelectIndex.value);
  }

  GlobalKey<ScaffoldState> _homeScaffoldKey = new GlobalKey();

  GlobalKey<ScaffoldState> get homeScaffoldKey => _homeScaffoldKey;

  //当前选中的索引
  RxInt _currentSelectIndex = 0.obs;

  int get currentSelectIndex => _currentSelectIndex.value;

  //菜单选项
  List<String> _menuList = ["Home", "Vue", "Flutter", "React", "About"];

  List<String> get menuList => _menuList;

  void updateSelectIndex(int index) {
    Logger().e(index);
    //更新
    _currentSelectIndex.value = index;
    //更新列表数据
    updateBlogList(index);
  }

  //当前显示类目的文章数据
  RxList _blogList = [].obs;

  RxList get blogList => _blogList;

  //存储不同分类的文章列表
  Map<int, List<Blog>> map = new Map();

  void updateBlogList(int index) async {
    //先从map中获取
    List<Blog>? list = map[index];
    if (list == null || list.length == 0) {
      list = [];
      String menu = _menuList[index];
      //清空当前页面显示数据
      _blogList.value = [];
      //模拟网络请求
      await Future.delayed(Duration(seconds: 1));
      for (int i = 0; i < 10; i++) {
        Blog bean = Blog();
        bean.title = "$menu-$i-旧城以西";
        bean.description =
            "Mobile banking has seen a huge increase since Coronavirus. In fact, CX platform Lightico found that 63 percent of people surveyed said they were more willing to try a new digital banking app than before the pandemic.So while you may be more inclined to bank remotely these days, cybercrime—especially targeting banks—is on the rise.";
        bean.image = "assets/images/banner.png";
        bean.date = '23 September 2020';
        if (index % 2 == 0) {
          bean.image = "assets/images/banner.png";
        }
        list.add(bean);
      }
      map[index] = list;
    }
    _blogList.value = list;
  }
}
